package mvp.com.bbc.rcjavaretrofitdaggermvp.mvp.wihget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by ${cbf} on 2017/11/27.
 * 自定义贝塞尔
 */

public class BezierView extends View {

    private Paint mPaint;
    private Point startpoint;
    private Point endPoint;
    private Point assentPoint;
    private Path path;

    public BezierView(Context context) {
        super(context, null);
    }

    public BezierView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        //防抖动
        mPaint.setDither(true);
        //抗锯齿
        mPaint.setAntiAlias(true);
        startpoint = new Point(300, 600);
        endPoint = new Point(900, 600);
        assentPoint = new Point(600, 900);
        path = new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        path.reset();
        //起点
        path.moveTo(startpoint.x, startpoint.y);
        //这句很重要  进行连接起来
        path.quadTo(assentPoint.x, assentPoint.y, endPoint.x, endPoint.y);
        //画路径
        canvas.drawPath(path, mPaint);
        //画辅助点
        canvas.drawPoint(assentPoint.x, assentPoint.y, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                assentPoint.x = (int) event.getX();
                assentPoint.y = (int) event.getY();
                Log.v("cbf", "111==" + assentPoint.x + "2222+" + assentPoint.y);
                invalidate();
                break;
        }
        return true;
    }
}
